'''
商户管理： 新增商户
'''
from Lib.base.keywords import KeyWords
from selenium.webdriver.common.by import By
from Lib.common.common_function import *
from Lib.base.page_object.login_page import LonginPage


class BusinessList(KeyWords):
    # 商户列表
    url = KeyWords.url + 'business/kerbsideList'

    # -----------------------筛选商户信息------------------------------
    # 商户状态
    business_status = (By.XPATH, '//span[text()="商户状态"]/../div/div/input')
    business_status_value1 = (By.XPATH, '//div[@x-placement="bottom-start"]//span[text()="持证经营"]')
    business_status_value2 = (By.XPATH, '//div[@x-placement="bottom-start"]//span[text()="不限"]')
    # 商户等级
    business_level = (By.XPATH, '//input[@placeholder="请选择商户等级"]')
    business_level_value1 = (By.XPATH, '//div[@x-placement="bottom-start"]//span[text()="低风险"]')
    business_level_value2 = (By.XPATH, '//div[@x-placement="bottom-start"]//span[text()="不限"]')
    # 违法次数
    business_case_number_start = (By.XPATH, '//span[text()="违法次数"]/../div[3]/input')
    business_case_number_end = (By.XPATH, '//span[text()="违法次数"]/../div[4]/input')
    # 断言元素
    list_assert = (By.XPATH, '//div[@class="card-grid"]/div[1]//label')

    # --------------------------搜索商户并查看详情和关联视频源---------------------------------
    # 根据关键字进行搜索
    business_search_input = (By.XPATH, '//input[contains(@placeholder, "请输入商户名称")]')
    business_search_button = (By.XPATH, '//i[contains(@class, "rz-search-input-suffix rz-icon-search")]')
    business_search_clear = (By.XPATH, '//i[contains(@class, "rz-input__clear")]')
    # 查看商户详情
    business_detail_button = (By.XPATH, '//span[text()="UI自动化商户"]')
    # 关联视频源
    business_and_video = (By.XPATH, '//span[text()="关联视频源"]')
    business_and_video_value = (
        By.XPATH, '//div[contains(@class,"is-expanded")]/div[1]//span[@class="rz-checkbox__inner"]')
    business_and_video_sure = (By.XPATH, '//span[text()="确定"]')
    # 断言元素
    detail_assert = (By.XPATH, '//p[text()="关联视频源成功"]')

    # ----------------------------编辑商户--------------------------------------
    business_update_button = (By.XPATH, '//span[text()="编辑"]')
    business_update_name = (By.XPATH, '//label[text()="商户名称"]/..//input')
    # 修改商户等级
    business_update_level = (By.XPATH, '//input[@placeholder="请选择商户等级"]')
    business_update_level_choose = (By.XPATH, '//span[text()="高风险"]')
    business_update_sure = (By.XPATH, '//span[text()="确定"]')
    # 断言元素
    update_assert = (By.XPATH, '//p[text()="编辑商户成功"]')
    # -----------------------------删除商户-------------------------------------
    # 定位修改名称后的商户
    business_already_update_name = (By.XPATH, '//span[text()="UI更改后的名称"]')
    # 删除商户
    business_delete_button = (By.XPATH, '//span[text()="删除"]')
    business_delete_cancel = (By.XPATH, '//div[@class="rz-message-box__btns"]//span[contains(text(), "取消")]')
    business_delete_sure = (By.XPATH, '//div[@class="rz-message-box__btns"]//span[contains(text(), "确定")]')
    # 断言元素
    delete_assert = (By.XPATH, '//p[text()="商户删除成功"]')

    # 查询商户列表
    def business_list(self):
        self.open(url=self.url)
        self.wait(3)
        # 根据商户状态进行筛选
        self.click(args=self.business_status, context="商户状态")
        self.wait(1)
        self.click(args=self.business_status_value1, context="持证经营")
        self.wait(2)
        self.click(args=self.business_status, context="商户状态")
        self.wait(1)
        self.click(args=self.business_status_value2, context="不限")
        # 根据商户等级进行筛选
        self.click(args=self.business_level, context="商户等级")
        self.wait(1)
        self.click(args=self.business_level_value1, context="低风险")
        self.wait(2)
        self.click(args=self.business_level, context="商户等级")
        self.wait(1)
        self.click(args=self.business_status_value2, context="不限")
        self.wait(1)
        # 筛选违法次数
        self.input_value(args=self.business_case_number_start, text="2", context="违法次数")
        self.input_value(args=self.business_case_number_end, text='5', context="违法次数")
        self.wait(2)
        return self.list_assert

    # 查询商户并查看详情和关联视频源
    def business_search_and_detail(self):
        self.open(url=self.url)
        self.wait(2)
        # 按地址进行搜索
        self.input_value(args=self.business_search_input, text="上海", context="商户地址")
        self.click(args=self.business_search_button, context="搜索按钮")
        self.wait(2)
        self.hover(args=self.business_search_input, context="搜索框")
        self.click(args=self.business_search_clear, context="清空搜索框")
        self.wait(2)
        # 按商户名称进行模糊搜索
        self.input_value(args=self.business_search_input, text="UI", context="商户地址")
        self.click(args=self.business_search_button, context="搜索按钮")
        self.wait(2)
        self.hover(args=self.business_search_input, context="搜索框")
        self.click(args=self.business_search_clear, context="清空搜索框")
        self.wait(2)
        # 按商户名称进行精确搜索
        self.input_value(args=self.business_search_input, text="UI自动化商户", context="商户地址")
        self.click(args=self.business_search_button, context="搜索按钮")
        self.wait(2)
        self.click(args=self.business_detail_button, context="商户名称")
        self.wait(1)
        # 关联视频源
        self.click(args=self.business_and_video, context="关联视频源")
        self.wait(1)
        self.click(args=self.business_and_video_value, context="选择视频源")
        self.wait(1)
        # 点击确认
        self.click(args=self.business_and_video_sure, context="确定")
        return self.detail_assert

    # 查询商户并编辑
    def business_search_and_update(self):
        self.open(url=self.url)
        self.wait(2)
        # 按商户名称进行精确搜索
        self.input_value(args=self.business_search_input, text="UI自动化商户", context="商户地址")
        self.click(args=self.business_search_button, context="搜索按钮")
        self.wait(2)
        self.click(args=self.business_detail_button, context="商户名称")
        self.wait(1)
        # 编辑商户信息
        self.click(args=self.business_update_button, context="编辑")
        self.wait(1)
        # 修改商户名称
        self.clear_input(args=self.business_update_name, context="商户名称")
        self.input_value(args=self.business_update_name, text="UI更改后的名称", context="商户名称")
        # 修改商户等级
        self.click(args=self.business_update_level, context="商户等级")
        self.wait(1)
        self.click(args=self.business_update_level_choose, context="高风险")
        self.wait(1)
        self.click(args=self.business_update_sure, context="确定")
        return self.update_assert

    def business_search_and_delete(self):
        self.open(url=self.url)
        self.wait(2)
        # 按商户名称进行精确搜索
        self.input_value(args=self.business_search_input, text="UI更改后的名称", context="商户地址")
        self.click(args=self.business_search_button, context="搜索按钮")
        self.wait(2)
        self.click(args=self.business_already_update_name, context="商户名称")
        self.wait(1)
        # 编辑商户信息
        self.click(args=self.business_delete_button, context="删除")
        self.wait(1)
        self.click(args=self.business_delete_cancel, context="取消")
        self.wait(1)
        self.click(args=self.business_delete_button, context="删除")
        self.wait(1)
        self.click(args=self.business_delete_sure, context="确定")
        return self.delete_assert


if __name__ == '__main__':
    driver = choose_browser()
    L = LonginPage(driver)
    L.login('kobeAdmin002', 'kobe8888')
    case = BusinessList(driver)
    case.business_search_and_update()
